﻿Imports Infragistics.Web.UI.NavigationControls

Public Class MyScouts
    Inherits System.Web.UI.Page
    Dim et As New ETModelContainer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not IsPostBack Then

            Dim org = From o In et.Organizations Where o.ParentID = 0 Or o.ParentID Is Nothing

            lstScouts.DataBind()
        End If
    End Sub

    Protected Sub cmdNew_Click(sender As Object, e As EventArgs) Handles cmdNew.Click

        Dim p As New Scout()

        p.UserID = User.Identity.Name
        p.FullName = "New Scout"
        p.UserID = User.Identity.Name
        et.AddToScouts(p)
        et.SaveChanges()

        lstScouts.DataBind()

    End Sub

    Private Sub lstScouts_DataBinding(sender As Object, e As System.EventArgs) Handles lstScouts.DataBinding

        Dim data = From x In et.Scouts Where x.UserID = User.Identity.Name Select x
        lstScouts.DataSource = data



    End Sub

    Private Function FindNode(node As DataTreeNode, ID As Integer) As DataTreeNode
        If CInt(node.Value) = ID Then
            Return node
        Else
            Dim foundnode As DataTreeNode = Nothing
            For Each c As DataTreeNode In node.Nodes
                foundnode = FindNode(c, ID)
                If Not foundnode Is Nothing Then
                    Return foundnode
                End If
            Next

            Return foundnode
        End If


    End Function

    Private Sub lstScouts_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstScouts.SelectedIndexChanged
        Dim scout As Scout = (From p In et.Scouts Where p.ScoutID = CInt(lstScouts.SelectedValue)).SingleOrDefault

        txtName.Text = scout.FullName
        txtEmail.Text = scout.Email
        For Each n As DataTreeNode In trvOrgs.SelectedNodes
            n.Selected = False
        Next
        If Not scout.OrganizationID Is Nothing Then
            Dim node As DataTreeNode = FindNode(trvOrgs.Nodes(0), CInt(scout.OrganizationID))
            node.Selected = True
            node.ExpandAnscestors()
        End If
    End Sub
    Private Sub PopulateCbo(cbo As DropDownList, list As IQueryable(Of Organization))
        cbo.DataSource = list
        cbo.DataTextField = "OrganizationName"
        cbo.DataValueField = "OrganizationID"
        cbo.DataBind()



    End Sub
    Private Function GetOrg(OrgID As Integer) As Organization
        Dim org As Organization = (From o In et.Organizations Where o.OrganizationID = OrgID).SingleOrDefault
        Return org

    End Function

    Private Function getSiblingUnits(ParentID As Integer) As IQueryable(Of Organization)

        Dim org As IQueryable(Of Organization) = (From o In et.Organizations Where o.ParentID = ParentID)



        Return org

    End Function


    Protected Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
        Dim scout As Scout = (From p In et.Scouts Where p.ScoutID = CInt(lstScouts.SelectedValue)).SingleOrDefault
        If chkDel.Checked Then
            et.Scouts.DeleteObject(scout)
        Else
            scout.FullName = txtName.Text
            scout.Email = txtEmail.Text

            scout.UserID = User.Identity.Name
            scout.OrganizationID = CType(trvOrgs.SelectedNodes().First.Value, Integer?)


        End If
        et.SaveChanges()
        lstScouts.DataBind()
        If Not chkDel.Checked Then
            lstScouts.SelectedIndex = lstScouts.Items.IndexOf(lstScouts.Items.FindByValue((scout.ScoutID).ToString))
        Else

            txtName.Text = ""
            txtEmail.Text = ""



            chkDel.Checked = False
        End If
    End Sub


    Private Sub UpdateNextLevel(DDL As DropDownList, ParentID As Integer)

        Dim org = From o In et.Organizations Where o.ParentID = ParentID
        DDL.DataSource = org
        DDL.DataTextField = "OrganizationName"
        DDL.DataValueField = "OrganizationID"
        DDL.DataBind()
        DDL.Items.Insert(0, "")


    End Sub





End Class